<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-3-26
 * Time: 下午3:45
 * 九天鼓浪屿轮渡票类修改通知接口
 * METHOD:POST
 * PARAMS:
 * auth:9abaee9bfd832c62353bfabbf8949a83
 * id:票ID
 * 包含文件：http://max.12301.cc/LD/RSYNC 
 */
if (!$arr['number'] || !$arr['id']) {
    exit('参数错误！');
}
echo "aaa";exit;  

$post_uuid   = trim($arr['number']).'_'.trim($arr['id']);

include '../../common/func.inc.php';
include '../../class/Ferry_gly.class.php';
include '../../api/ProductApi.php';
$pdo = new PDO('mysql:dbname=myuu;host=127.0.0.1;port=3306', 'admin', 'adm*753951');
$pdo->exec('SET NAMES "UTF8"');

$obj = new Ferry_gly();
$xml = $obj->getFlightLine();
$data_list = simplexml_load_string($xml)->content->list;
// print_r($data_list);exit;
$apply_did = 27491;//厦门鼓浪屿轮渡
$price_beg_date = date('Y-m-d', $_SERVER['REQUEST_TIME']);
$price_end_date = date('Y-m-d', strtotime('+5 years'));
$product = new ProductApi($pdo);
$cnt = 0;
foreach ($data_list->flightLine as $body) {
    $landInfo   = array();//uu_land表数据
    $extInfo    = array();
    $ticketInfo = array();//uu_jq_ticket表数据
    $landInfo['title']    = (string)$body->name;
    $landInfo['p_type']   = 'A';
    $landInfo['qt']       = (string)$body->number;
    $landInfo['jdjj']     = '出发港口:' .(string)$body->departProt
                           .PHP_EOL . '抵达港口:' .(string)$body->arrivePort
                           .PHP_EOL . '早班时间:' . (string)$body->firstFlightTime
                           .PHP_EOL . '晚班时间:' . (string)$body->lastFlightTime
                           .PHP_EOL . '提前预售时间:' . (string)$body->advanceSaleDate
                           .PHP_EOL . '取消费用:' . htmlspecialchars_decode((string)$body->poundageAmount);

    $landInfo['opentime'] = substr((string)$body->firstFlightTime,10) . '至'
                           . substr((string)$body->lastFlightTime,10);
    $landInfo['status']    = 1;
    $landInfo['apply_did'] = $apply_did;

    $landInsertRes   = $product->AddProduct($landInfo);
    $lid             = $landInsertRes['lastid'];
//    if ((string)$body->number!='DS') {
//        continue;
//    }
    //exit;
    foreach ($body->ticketList->ticket as $ticket) {
        $tid = 0;
        //check exist or not
        $uuid = strval($body->number).'_'.strval($ticket->id);
        
        if ($post_uuid!=$uuid) break;

        $sql_chk = "SELECT id FROM uu_jq_ticket where uuid=? LIMIT 1";
        $stmt = $pdo->prepare($sql_chk);
        $stmt->execute(array($uuid));
        $tid = $stmt->fetchColumn(0);

        $ticketInfo['landid']       = $landInsertRes['lastid'];
        $ticketInfo['title']        = (string)$ticket->name;
        $ticketInfo['uuid']         = $uuid;
        $ticketInfo['pay']          = 1;//在线支付
        $ticketInfo['apply_did']    = $apply_did;
        $ticketInfo['buy_limit_low']= 1;
        $ticketInfo['getaddr']= '出发港口:' .(string)$body->departProt
            . ';抵达港口:' .(string)$body->arrivePort;
//        print_r($ticketInfo);
//        exit;
        $ticketInsertRet = $product->SaveTicket($ticketInfo, $tid);
        if ($ticketInsertRet===false) {
            ProductApi::Response('115');//保存票类属性失败
        }

        //TODO::写数据到票类属性扩展表
        $extInfo['pid'] = $ticketInsertRet['pid'];
        $extInfo['tid'] = $ticketInsertRet['lastid'];
        $extInfo['lid'] = $landInsertRes['lastid'];
        $extInfo['startplace']      = (string)$body->departProt;//
        $extInfo['endplace']        = (string)$body->arrivePort;//

        $ticket_ext = $product->ProductExt($extInfo);

        //判定这个价格是否已经写入到数据库
        $mode       = 0;
        $rid        = 0;
        $sql_get_priceid = <<<SQL
SELECT id FROM uu_product_price WHERE pid=? LIMIT 1
SQL;
        $stmt = $pdo->prepare($sql_get_priceid);
        $stmt->execute(array($ticketInsertRet['pid']));
        $price_id = $stmt->fetchColumn(0);
        if ($price_id) {
            $mode = 1;
            $rid = $price_id;
        }
        $priceRes  = ProductApi::soap()->In_Dynamic_Price_Merge(
            $ticketInsertRet['pid'],
            $price_beg_date,
            $price_end_date,
            (int)$ticket->price*100,
            (int)$ticket->price*100,
            0,
            $mode,
            $rid,
            '',
            '0,1,2,3,4,5,6',
            -1);

        if ($priceRes==142) {
            ProductApi::Response('122');//122：一个票种多个价格有效期日期重叠
        }
        elseif ($priceRes!=100) {
            ProductApi::writeLog(ProductApi::now()."|价格插入失败.返回{$priceRes}",
                'logs/price.err');
            ProductApi::Response('100');
        }
        $product->SetTicketState($ticketInsertRet['pid'], 0);
        echo $ticket->name,';;;;;;',$ticket->id,';;;;;;',$tid,';;;添加成功',PHP_EOL;
    }
}






